using ExtendedMvc.Common.Diagnostics;
using ExtendedMvc.Html;

namespace ExtendedMvc.Elements {
	public class TextBox<TModel> : TextBoxBase<TextBox<TModel>, TModel> where TModel : class {
		public TextBox(IModelViewHelper<TModel> helper, string memberName, object value, string id = null) : base(helper, HtmlInputType.Text, memberName, value, id) { }
	}

	public class Password<TModel> : TextBoxBase<Password<TModel>, TModel> where TModel : class {
		public Password(IModelViewHelper<TModel> helper, string memberName, string id = null) : base(helper, HtmlInputType.Password, memberName, string.Empty, id) { }

		public override Password<TModel> Value(object value) {
			return base.Value(null);
		}
	}

	public abstract class TextBoxBase<T, TModel> : Input<T, TModel>
		where T : TextBoxBase<T, TModel>
		where TModel : class {
		private int? _maxLength;
		private int? _size;

		protected TextBoxBase(IModelViewHelper<TModel> helper, string type, string memberName, object value, string id = null) : base(helper, type, value, memberName, id) { }

		public virtual T MaxLength(int value) {
			Check.Argument.IsNotNegative(value, "value");
			_maxLength = value == 0 ? (int?)null : value;
			return (T)this;
		}

		public virtual T Size(int value) {
			Check.Argument.IsNotNegative(value, "value");
			_size = value == 0 ? (int?)null : value;
			return (T)this;
		}

		protected override void Prepare() {
			if (_maxLength != null) SetAttribute(HtmlAttribute.MaxLength, _maxLength.Value);
			if (_size != null) SetAttribute(HtmlAttribute.Size, _size.Value);
			base.Prepare();
		}
	}
}
